package cn.kgc.shiroday03.controller;

import cn.kgc.shiroday03.annotations.OperationLog;
import cn.kgc.shiroday03.commons.CodeMsg;
import cn.kgc.shiroday03.commons.Operation;
import cn.kgc.shiroday03.commons.ResponseResult;
import cn.kgc.shiroday03.dto.UserFormDTO;
import cn.kgc.shiroday03.dto.UserLoginDTO;
import cn.kgc.shiroday03.dto.UserPageDTO;
import cn.kgc.shiroday03.entity.User;
import cn.kgc.shiroday03.service.UserService;
import io.swagger.annotations.*;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authz.AuthorizationException;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.Map;

/**
 * @Author: 课工场
 * @Version: v1.0  2023/6/3
 * @Description:
 */
@RestController
@RequestMapping("user")
@Api(tags = "用户模块")
public class UserController {

    @Resource
    private UserService userService;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @GetMapping("all")
    @ApiOperation("用户分页")
    public ResponseResult getPageList(UserPageDTO userPageDTO) {
        return userService.getPage(userPageDTO);
    }

    // 登录操作
    @GetMapping("login")
    @ApiOperation("登录")
    public ResponseResult login(UserLoginDTO userLoginDTO, HttpSession session) {

        UsernamePasswordToken usernamePasswordToken = new UsernamePasswordToken(userLoginDTO.getUsername(), userLoginDTO.getPassword());
        Subject subject = SecurityUtils.getSubject();

        //验证码
        String id = session.getId();
        String code = stringRedisTemplate.opsForValue().get(id);

        String captcha = userLoginDTO.getCaptcha();

        // 判定  验证码是否过期
        if(ObjectUtils.isEmpty(code)){
            return  new ResponseResult(CodeMsg.CAPTCHA_OUT_EXPIRE);
        }

        if (!code.equals(captcha)) {
            return  new ResponseResult(CodeMsg.CAPTCHA_ERROR);
        }

        // 判定用户是否勾一周免登录
        String rememberMe = userLoginDTO.getRememberMe();
        if(!ObjectUtils.isEmpty(rememberMe)){
            usernamePasswordToken.setRememberMe(true);
        }


        // 用户认证
        subject.login(usernamePasswordToken);

        return new ResponseResult(CodeMsg.USER_LOGIN_SUCCESS);

    }


    // 退出登录
    @GetMapping("logout")
    @ApiOperation("退出")
    public ResponseResult logout(){
        Subject subject = SecurityUtils.getSubject();
        subject.logout();
        return  new ResponseResult(CodeMsg.SUCCESS);
    }


    @PostMapping("add")
    @RequiresPermissions("user:add")
    @OperationLog(description = Operation.ADD,model = "用户模块")
    @ApiOperation("添加用户")
    public ResponseResult add(@RequestBody UserFormDTO userFormDTO) {

        return userService.addUser(userFormDTO);
    }

    @PostMapping("state")
    @ApiOperation("更改用户状态")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "用户id",required = true,dataType ="Integer" ),
            @ApiImplicitParam(name = "state",value = "用户状态",required = true,dataType ="Integer" )
       }
    )
    public ResponseResult updateUserState(Integer id,Integer state){

        User user = new User();
        user.setId(id);
        user.setState(state);
        userService.updateById(user);
        return  new ResponseResult(CodeMsg.SUCCESS);
    }


    @GetMapping("getById")
    @ApiOperation("根据id获取用户信息")
    public ResponseResult getById(@ApiParam(value = "用户id",required = true) Integer id){

        return  userService.getUserById(id);

    }


    @PutMapping("update")
    @OperationLog(description = Operation.UPDATE,model = "用户模块")
    public ResponseResult updateUser(@RequestBody UserFormDTO userFormDTO){
        Subject subject = SecurityUtils.getSubject();

        // 1.编程式  实现权限的判定
        if(subject.hasRole("super")) {
            return  userService.updateUser(userFormDTO);
        }

       throw new AuthorizationException();
    }

    @DeleteMapping("delete")
    @RequiresRoles("super")      // 1.注解式 权限判定
    @RequiresPermissions("user:delete")  //   2.根据权限字符完成权限认证
    @OperationLog(description = Operation.DELETE,model = "用户模块")
    @ApiOperation("删除用户信息")
    public ResponseResult delete(Integer[] ids){
        return  userService.deleteById(ids);
    }

    @GetMapping("loadMenu")
    @ApiOperation("动态菜单加载")
    public Map<String,Object> loadMenu(@ApiParam(value = "用户名",required = true) String username){

       return userService.loadMenu(username);

    }
}

